﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Model;

namespace 超市收银台
{
    public partial class Zhumianban : Form
    {
        public Zhumianban()
        {
            InitializeComponent();
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                txtSearch.Focus();
            }
        }
        private void Search()
        {
            MessageBox.Show("结算成功");
        }
        
        private void txtSearch_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F5)
            {
                Search();
            }
            if (txtSearch.Text != "") 
            {
                if (e.KeyCode == Keys.Enter)
                {
                    btnSearch_Click(null, null);                    
                }
            }
        }
        private List<BuyGoodsInfo> buygoods = new List<BuyGoodsInfo>();
        private void bangdin()
        {
            dgvInfo.DataSource = new BindingList<BuyGoodsInfo>(buygoods);
        }
        private void btnSearch_Click(object sender, EventArgs e)
        {
                GoodsService gs = new GoodsService();
                List<GoodsInfo> add =  gs.GService(txtSearch.Text);
                BuyGoodsInfo a = new BuyGoodsInfo();
            if (add.Count == 0)
            {
                MessageBox.Show("商品码错误");
                txtSearch.Text = "";
                return;
            }
            else if(add.Count == 1)
            {
               a.GoodsName = add[0].GoodsName;
               a.SalePrice = add[0].SalePrice;
               a.count = 1;
               buygoods.Add(a);
            }
            else
            {
                    GoodsBuy gbu = new GoodsBuy(add);
                    gbu.ShowDialog();
                    int c = gbu.Goods;
                    try
                    {
                        a.GoodsName = add[c].GoodsName;
                        a.SalePrice = add[c].SalePrice;
                        a.count = 1;
                        buygoods.Add(a);
                        txtSearch.Text = "";
                    }
                    catch (Exception)
                    {
                        txtSearch.Text = "";
                        return;
                    }
             }

            bangdin();
        }
        private void GoodsMarket_Load(object sender, EventArgs e)
        {
            dgvInfo.AutoGenerateColumns = false;

        }

        private void Zhumianban_Load(object sender, EventArgs e)
        {
            dgvInfo.AutoGenerateColumns = false;
        }
    }
}
